﻿
using MatTrace.Inputs;
using System;
using System.Collections.Generic;
using System.Text;
using System.Threading.Tasks;
using Volo.Abp.DependencyInjection;
using Volo.Abp.Domain.Repositories;
using Volo.Abp.Domain.Services;
using Volo.Abp.ObjectMapping;
using System.Linq;


namespace MatTrace 
{


    /// <summary>
    /// 物料跟踪逻辑
    /// </summary>
    public class MatTraceManager: DomainService
    {


        public ITmrbr001DapperRepository tmrbr001DapperRepository { get; set; }

        public ITmrbr096DapperRepository tmrbr096DapperRepository { get; set; }

        public ITmrbr096Repository tmrbr096Repository { get; set; }

        public ITmrbr001Repository tmrbr001Repository { get; set; }





        public IObjectMapper objectMapper { get; set; }


        

        public MatTraceManager(
            ITmrbr001DapperRepository tmrbr001DapperRepository, 
            ITmrbr096DapperRepository tmrbr096DapperRepository,
            ITmrbr096Repository tmrbr096Repository,
            ITmrbr001Repository tmrbr001Repository,
            IObjectMapper objectMapper
            )
        {

            this.tmrbr001DapperRepository = tmrbr001DapperRepository;
            this.tmrbr096DapperRepository = tmrbr096DapperRepository;
            this.tmrbr096Repository = tmrbr096Repository;
            this.tmrbr001Repository = tmrbr001Repository;

            this.objectMapper = objectMapper;

        }






        /// <summary>
        /// 查询可做准发的计划
        /// </summary>
        /// <param name="queryTmrbr001Input"></param>
        /// <returns></returns>
        public virtual async Task<List<Tmrbr001>> Query_001(QueryTmrbr001Input queryTmrbr001Input)
        {


           return await tmrbr001Repository.GetTmrbr001ConfirmPlan(queryTmrbr001Input);

        }



        /// <summary>
        /// 准发逻辑
        /// </summary>
        /// <param name="MatNo"></param>
        /// <param name="PlanNo"></param>
        /// <returns></returns>
        public virtual async Task<MatTraceResult> Event_001(List<Trace001Input>   trace001Inputs)
        {

            //
            var rs = new MatTraceResult() { Message = "", Status = true };


            try
            {


                foreach (var trace001Input in trace001Inputs)
                {


                    var exist = await this.tmrbr001Repository.Exist_MatNo(trace001Input.MatNo);

                    if (!exist) throw new Exception($"{trace001Input.MatNo} is not exist in Tmrbr001");


                    var exist_status = await this.tmrbr001Repository.Exist_MatNoStatus(trace001Input.MatNo, "33");

                    if (!exist_status) throw new Exception($"{trace001Input.MatNo}  MatStatus is not equal 33");


                    await this.tmrbr001DapperRepository.UpdateBySql("Update Tmrbr001   SET  CONFM_PLAN_NO=@PlanNo, MAT_STATUS='34', CONFM_FLAG='1' WHERE MAT_NO=@MatNo ", new { MatNo = trace001Input.MatNo, PlanNo = trace001Input.PlanNo });

                    //执行备份

                    //映射001实体到096;


                    var tmrbr001 = await this.tmrbr001Repository.GetAsync(m => m.MatNo == trace001Input.MatNo);
                    // var tmrbr001 = await this.tmrbr001DapperRepository.FindSingleBySql("SELECT T.* FROM TMRBR001 T WHERE T.MAT_NO=@MatNo", new { MatNo = trace001Input.MatNo });

                    //设置映射到096
                    var tmrbr096 = objectMapper.Map<Tmrbr001, Tmrbr096>(tmrbr001);

                    await this.tmrbr096Repository.InsertAsync(tmrbr096);
                    //await this.tmrbr096DapperRepository.InsertBySelect("INSERT INTO TMRBR096 (SELECT T.* FROM Tmrbr001 T WHERE T.MAT_ID=:MatNo)");

                }
            }
            catch (Exception ex)
            {

                rs.Status = false;
                rs.Message = ex.Message;
            }


            return await Task.FromResult(rs);

        }

    }
}
